package cn.tedu.egweibo04.controller;

import cn.tedu.egweibo04.base.response.JsonResult;
import cn.tedu.egweibo04.base.response.StatusCode;
import cn.tedu.egweibo04.mapper.CommentMapper;
import cn.tedu.egweibo04.pojo.dto.CommentDTO;
import cn.tedu.egweibo04.pojo.entity.Comment;
import cn.tedu.egweibo04.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

import java.util.Date;

import static cn.tedu.egweibo04.base.response.StatusCode.COMMENT_FAILED;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "03.评论管理模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("/insert")
    @ApiOperation("发布评论")
    @ApiImplicitParam(name = "commentDTO", value = "评论信息", required = true, dataType = "CommentDTO")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) return new JsonResult(COMMENT_FAILED);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @GetMapping("/selectById/{wid}")
    @ApiOperation("查询指定微博的所有评论")
    @ApiImplicitParam(name = "wid", value = "微博id", required = true, dataType = "Integer")
    public JsonResult selectById(@PathVariable Long wid) {
        return JsonResult.ok(commentMapper.selectByWeiboId(wid));
    }
}
